/*
 * $Id: JCreateMapDialog.java 24 2009-03-31 23:40:10Z aaron.day $
 */
package org.crazydays.gameplan.map.swing;


import java.awt.Frame;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.GroupLayout.Alignment;

import org.crazydays.gameplan.GamePlan;
import org.crazydays.gameplan.map.GameMap;
import org.crazydays.gameplan.map.GridType;
import org.crazydays.gameplan.map.IGameMap;
import org.crazydays.gameplan.map.swing.event.CreateMapAction;

/**
 * JCreateMap
 */
public class JCreateMapDialog
        extends JDialog
{
	/** serial version uid */
	public final static long serialVersionUID =
	        "$Id: JCreateMapDialog.java 24 2009-03-31 23:40:10Z aaron.day $"
	                .hashCode();

	/** game plan */
	protected GamePlan gamePlan;

	/** name label */
	protected JLabel nameLabel;

	/** name input */
	protected JTextField nameInput;

	/** type label */
	protected JLabel typeLabel;

	/** type input */
	protected JComboBox typeInput;

	/** height label */
	protected JLabel heightLabel;

	/** height input */
	protected JSpinner heightInput;

	/** width label */
	protected JLabel widthLabel;

	/** width input */
	protected JSpinner widthInput;

	/** create */
	protected JButton create;

	/**
	 * JCreateMap constructor.
	 * 
	 * @param gamePlan Game plan
	 */
	public JCreateMapDialog(GamePlan gamePlan)
	{
		super((Frame) gamePlan.getParent(), "Create Map", true);

		setGamePlan(gamePlan);

		initialize();
	}

	/**
	 * Initialize the form components.
	 */
	protected void initialize()
	{
		initializeName();
		initializeType();
		initializeHeight();
		initializeWidth();
		initializeCreate();
		initializeLayout();
		pack();
	}

	/**
	 * Initialize name.
	 */
	protected void initializeName()
	{
		// label
		nameLabel = new JLabel("Name");

		// text field
		nameInput = new JTextField("");
	}

	/**
	 * Initialize the map type components.
	 */
	protected void initializeType()
	{
		// label
		typeLabel = new JLabel("Map Type");

		// combo box
		typeInput = new JComboBox(GridType.values());
		typeInput.setSelectedIndex(0);
	}

	/**
	 * Initialize map height coponents.
	 */
	protected void initializeHeight()
	{
		// label
		heightLabel = new JLabel("Height Label");

		// input
		heightInput =
		        new JSpinner(
		                new SpinnerNumberModel(480, 200, 640000, 1));
	}

	/**
	 * Initialize map width coponents.
	 */
	protected void initializeWidth()
	{
		// label
		widthLabel = new JLabel("Width Label");

		// input
		widthInput =
		        new JSpinner(
		                new SpinnerNumberModel(640, 200, 640000, 1));
	}

	/**
	 * Initialize create component.
	 */
	protected void initializeCreate()
	{
		create =
		        new JButton(new CreateMapAction("Create", null, null,
		                null, this));

		// set as default button
		getRootPane().setDefaultButton(create);
	}

	/**
	 * Initialize component layout.
	 */
	protected void initializeLayout()
	{
		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);

		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		// horizontal
		GroupLayout.SequentialGroup horizontal =
		        layout.createSequentialGroup();

		horizontal.addGroup(layout.createParallelGroup().addComponent(
		        nameLabel).addComponent(typeLabel).addComponent(
		        heightLabel).addComponent(widthLabel));
		horizontal.addGroup(layout
		        .createParallelGroup(Alignment.CENTER).addComponent(
		                nameInput).addComponent(typeInput)
		        .addComponent(heightInput).addComponent(widthInput)
		        .addComponent(create));
		layout.setHorizontalGroup(horizontal);

		// vertical
		GroupLayout.SequentialGroup vertical =
		        layout.createSequentialGroup();
		vertical.addGroup(layout
		        .createParallelGroup(Alignment.BASELINE).addComponent(
		                nameLabel).addComponent(nameInput));
		vertical.addGroup(layout
		        .createParallelGroup(Alignment.BASELINE).addComponent(
		                typeLabel).addComponent(typeInput));
		vertical.addGroup(layout
		        .createParallelGroup(Alignment.BASELINE).addComponent(
		                heightLabel).addComponent(heightInput));
		vertical.addGroup(layout
		        .createParallelGroup(Alignment.BASELINE).addComponent(
		                widthLabel).addComponent(widthInput));
		vertical.addGroup(layout
		        .createParallelGroup(Alignment.BASELINE).addComponent(
		                create));
		layout.setVerticalGroup(vertical);
	}

	/**
	 * Set game plan.
	 * 
	 * @param gamePlan Game plan
	 */
	public void setGamePlan(GamePlan gamePlan)
	{
		this.gamePlan = gamePlan;
	}

	/**
	 * Get game plan.
	 * 
	 * @return Game plan
	 */
	public GamePlan getGamePlan()
	{
		return gamePlan;
	}

	/**
	 * Create map.
	 */
	public void createMap()
	{
		String name = nameInput.getText();
		GridType type = GridType.values()[typeInput.getSelectedIndex()];
		int height = (Integer) heightInput.getValue();
		int width = (Integer) widthInput.getValue();

		IGameMap gameMap = new GameMap(name, height, width, type, 20);

		getGamePlan().openGameMap(gameMap);

		setVisible(false);
	}
}
